package com.example.module.custom.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.example.module.custom.R;
import com.example.module.custom.utils.Utils;

import java.lang.reflect.Method;

/**
 * 创建时间：2017/06/08
 * 创建者: taoyong
 * 模块描述：PopupWindow工具类
 */
public class PopUtils {
    private static String TAG = "PopUtils";

    private PopUtils() {
    }

    /**
     * 创建一个空的PopupWindow
     */
    private static PopupWindow pop = null;
    private static PopupWindow popup = null;

    /**
     * 创建一个空的PopupWindow
     */
    private static boolean isAdapt = false;

    /**
     * 判断PopupWindow是否显示
     *
     * @return
     */
    public static boolean isPopShowing() {
        if (pop != null) {
            return pop.isShowing();
        }
        return false;
    }
    /**
     * 判断PopupWindow是否显示
     *
     * @return
     */
    public static boolean isAdaptEvirtualKey() {
        return isAdapt;
    }


    /**
     * 显示PopupWindow
     *
     * @param v       随便传一个您的界面有的View
     * @param popView pop的布局
     * @param width   pop显示的宽度
     * @param height  pop显示的高度
     * @param offX    对于v而言的X偏移量
     * @param offY    对于v而言的Y偏移量
     * @param flag    是否有焦点
     */
    public static void showPopupWindow(final View v, View popView, int width, int height, int offX, int offY, boolean flag) {
        hidePopupWindow();
        // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
        pop = new PopupWindow(popView);
        pop.setWidth(width);
        //popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        pop.setHeight(height);
        // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
        pop.setFocusable(flag);
        //此处为popwindow 设置背景，同事做到点击外部区域，popwindow消失
        pop.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#99000000")));
        //将window视图显示在v的  指定位置
        pop.showAtLocation(v, Gravity.NO_GRAVITY, offX, offY);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
            }
        });
    }
    /**
     * @param activity      Activity
     * @param attachOnView  显示在这个View的下方
     * @param popView       被显示在PopupWindow上的View
     * @param popShowHeight 被显示在PopupWindow上的View的高度，可以传默认值defaultBotom
     * @param popShowWidth  被显示在PopupWindow上的View的宽度，一般是传attachOnView的getWidth()
     * @return PopupWindow
     */
    public static void showDown(Activity activity, View attachOnView, View popView, final int popShowHeight, final int popShowWidth) {
        if (popView != null && popView.getParent() != null) {
            ((ViewGroup) popView.getParent()).removeAllViews();
        }
        int defaultBotom = -100;
        hidePopupWindow();
        // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
        pop = new PopupWindow(popView);
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int location[] = new int[2];
        int x, y;
        int popHeight = 0, popWidth = 0;
        attachOnView.getLocationOnScreen(location);
        x = location[0];
        y = location[1];
        int h = attachOnView.getHeight();
        int screenHeight = Utils.getScreenHeight(activity);
        if (popShowHeight == defaultBotom) {
            popHeight = screenHeight / 6;
            popHeight = Math.abs(screenHeight - (h + y)) - popHeight;
        } else if (popHeight == ViewGroup.LayoutParams.WRAP_CONTENT) {
            popHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
        } else {
            popHeight = popShowHeight;
        }
        if (popShowWidth == ViewGroup.LayoutParams.WRAP_CONTENT) {
            popWidth = attachOnView.getWidth();
        } else {
            popWidth = popShowWidth;
        }

        pop = new PopupWindow(popView, popWidth, popHeight, true);
        //这行代码时用来让PopupWindow点击区域之外消失的，这个应该是PopupWindow的一个bug。
        //但是利用这个bug可以做到点击区域外消失
        pop.setBackgroundDrawable(new BitmapDrawable());
        //popupWindow.setAnimationStyle(R.style.PopupAnimationDown);
       pop.showAtLocation(attachOnView, Gravity.NO_GRAVITY, x, h + y);
        //pop.showAsDropDown(attachOnView);
        pop.update();
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
            }
        });
    }
    /**

    /**
     * 显示PopupWindow
     *
     * @param activity
     * @param v        随便传一个您的界面有的View
     * @param popView  pop的布局
     * @param width    pop显示的宽度
     * @param height   pop显示的高度
     * @param offX     对于v而言的X偏移量
     * @param offY     对于v而言的Y偏移量
     * @param flag     是否有焦点
     */
    public static void showPopupWindow(final Activity activity, final View v, View popView, int width, int height, int offX, int offY, boolean flag, boolean isAdaptEvirtualKey) {
        hidePopupWindow();
        // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
        pop = new PopupWindow(popView);
        pop.setWidth(width);
        //popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        pop.setHeight(height);
//        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
        pop.setFocusable(flag);
        //此处为popwindow 设置背景，同事做到点击外部区域，popwindow消失
        pop.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#60000000")));
        //将window视图显示在v的  指定位置
        isAdapt = isAdaptEvirtualKey;
        pop.showAtLocation(v, Gravity.NO_GRAVITY, offX, offY);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
               /* if (v instanceof SideBar) {
                    ((SideBar) v).SideBarInvalidate();
                }
                if (activity_imessage instanceof ChatActivity) {
                    ((ChatActivity) activity_imessage).updateSoftInputMode(false);
                    ((ChatActivity) activity_imessage).deleteText();
                }
                if (activity_imessage instanceof MainActivity) {
                    ((MainActivity) activity_imessage).showChatMoImg();
                    ((MainActivity) activity_imessage).deleteText();
                }
                if (activity_imessage instanceof PaySettingActivity) {
                    ((PaySettingActivity) activity_imessage).showToggbtn();
                }*/
            }
        });
    }

    /**
     * 显示PopupWindow
     *
     * @param v       随便传一个您的界面有的View
     * @param popView pop的布局
     * @param width   pop显示的宽度
     * @param height  pop显示的高度
     * @param offX    对于v而言的X偏移量
     * @param offY    对于v而言的Y偏移量
     * @param flag    是否有焦点
     * @param color   背景颜色
     */
    public static void showPopupWindow(final View v, View popView, int width, int height, int offX, int offY, boolean flag, int color) {
        hidePopupWindow();
        // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
        pop = new PopupWindow(popView);
        pop.setWidth(width);
        //popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        pop.setHeight(height);
        // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
        pop.setFocusable(flag);
        //此处为popwindow 设置背景，同事做到点击外部区域，popwindow消失
        pop.setBackgroundDrawable(new ColorDrawable(color));
        //将window视图显示在v的  指定位置
        pop.showAtLocation(v, Gravity.NO_GRAVITY, offX, offY);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
            }
        });
    }

    /**
     * 隐藏PopupWindow
     */
    public static void hidePopupWindow() {
        //Log.i("TAG", "有没有进来消失的方法");
        if (pop != null) {
            //Log.i("TAG", "hidePopupWindow: 是不是不等于空");
            pop.dismiss();
            pop = null;
        }
    }

    /**
     * 显示PopupWindow
     *
     * @param activity
     * @param popView     随便传一个您的界面有的View
     *
     * @param isAnimation 是否需要动画  true 为需要
     */
    public static void showPopupWindow(final Activity activity, View popView, boolean isAnimation) {
        hidePopupWindow();
        // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
        int screenHeight = Utils.getScreenHeight(activity);
        int popupWindowHeight;
        if(Utils.isHUAWEI() && Utils.checkDeviceHasNavigationBar(activity)){
            int navigationBarHeight = Utils.getNavigationBarHeight(activity);
            popupWindowHeight = screenHeight - navigationBarHeight;
        }else {
            popupWindowHeight = WindowManager.LayoutParams.MATCH_PARENT;
        }
        pop = new PopupWindow(popView, WindowManager.LayoutParams.MATCH_PARENT, popupWindowHeight, true);
        //此处为popwindow 设置背景，同事做到点击外部区域，popwindow消失
        // pop.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));
        pop.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#60000000")));
        pop.setFocusable(true);
        pop.setClippingEnabled(false);//兼容沉浸式状态栏
        if (isAnimation) {
           // pop.setAnimationStyle(R.style.take_photo_anim);
        }
        pop.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        pop.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        //让键盘弹出时，不会挡住pop窗口。
        pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        //pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        WindowManager.LayoutParams params = activity.getWindow().getAttributes();
        params.alpha = 0.6f;
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        activity.getWindow().setAttributes(params);
        //将window视图显示在v的  指定位置
        pop.showAtLocation(popView, Gravity.CENTER, 0, 0);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = activity.getWindow().getAttributes();
                params.alpha = 1.0f;//设置为不透明，即恢复原来的界面
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                activity.getWindow().setAttributes(params);
                pop.dismiss();
            }
        });
    }
    /**
     * 显示PopupWindow
     *
     * @param activity
     * @param popView     随便传一个您的界面有的View
     *
     * @param isAnimation 是否需要动画  true 为需要
     */
    public static void showPopupWindowUpdat(final Activity activity, View popView, boolean isAnimation) {
        hidePopupWindow();
        // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
        int screenHeight = Utils.getScreenHeight(activity);
        int popupWindowHeight;
        if(Utils.isHUAWEI() && Utils.checkDeviceHasNavigationBar(activity)){
            int navigationBarHeight = Utils.getNavigationBarHeight(activity);
            popupWindowHeight = screenHeight - navigationBarHeight;
        }else {
            popupWindowHeight = WindowManager.LayoutParams.MATCH_PARENT;
        }
        pop = new PopupWindow(popView, WindowManager.LayoutParams.MATCH_PARENT, popupWindowHeight, true);
        //此处为popwindow 设置背景，同事做到点击外部区域，popwindow消失
        // pop.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));
        pop.setBackgroundDrawable(null);
        pop.setFocusable(true);
        pop.setOutsideTouchable(false);
        pop.setClippingEnabled(false);//兼容沉浸式状态栏
        if (isAnimation) {
           // pop.setAnimationStyle(R.style.take_photo_anim);
        }
        pop.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        pop.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        //让键盘弹出时，不会挡住pop窗口。
        pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        //pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        WindowManager.LayoutParams params = activity.getWindow().getAttributes();
        params.alpha = 0.6f;
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        activity.getWindow().setAttributes(params);
        //将window视图显示在v的  指定位置
        pop.showAtLocation(popView, Gravity.CENTER, 0, 0);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = activity.getWindow().getAttributes();
                params.alpha = 1.0f;//设置为不透明，即恢复原来的界面
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                activity.getWindow().setAttributes(params);
                pop.dismiss();
            }
        });
    }

    /**
     * 显示PopupWindow
     *
     * @param activity
     * @param popView     随便传一个您的界面有的View
     *
     * @param isAnimation 是否需要动画  true 为需要
     */
    public static void showPopupWindowURL(final Activity activity, View popView, boolean isAnimation) {
        hidePopupWindow();
        // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
        int screenHeight = Utils.getScreenHeight(activity);
        int popupWindowHeight;
        if(Utils.isHUAWEI() && Utils.checkDeviceHasNavigationBar(activity)){
            int navigationBarHeight = Utils.getNavigationBarHeight(activity);
            popupWindowHeight = screenHeight - navigationBarHeight;
        }else {
            popupWindowHeight = WindowManager.LayoutParams.MATCH_PARENT;
        }
        pop = new PopupWindow(popView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
        //此处为popwindow 设置背景，同事做到点击外部区域，popwindow消失
        // pop.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));
        //pop.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#60000000")));
        pop.setFocusable(false);
        pop.setOutsideTouchable(false);
        pop.setClippingEnabled(false);//兼容沉浸式状态栏
        if (isAnimation) {
           // pop.setAnimationStyle(R.style.take_photo_anim);
        }
        //让键盘弹出时，不会挡住pop窗口。
        pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        //pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //WindowManager.LayoutParams params = activity.getWindow().getAttributes();
       // params.alpha = 0.6f;
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        //activity.getWindow().setAttributes(params);
        //将window视图显示在v的  指定位置
        pop.showAtLocation(popView, Gravity.CENTER, 0, 0);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = activity.getWindow().getAttributes();
                params.alpha = 1.0f;//设置为不透明，即恢复原来的界面
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                activity.getWindow().setAttributes(params);
                pop.dismiss();
            }
        });
    }
    /**
     * 显示PopupWindow
     *    礼物
     * @param activity
     * @param popView     随便传一个您的界面有的View
     *
     * @param isAnimation 是否需要动画  true 为需要
     */
    public static void initPopupWindow(final Activity activity, View popView, boolean isAnimation) {
        int statusBarHeight =Utils.getStatusBarHeight(activity);
        int screenHeight = Utils.getScreenHeight(activity);
        int popupWindowHeight;
        if(Utils.isHUAWEI() && Utils.checkDeviceHasNavigationBar(activity)){
            int navigationBarHeight = Utils.getNavigationBarHeight(activity);
            popupWindowHeight = screenHeight - navigationBarHeight;
        }else {
            popupWindowHeight = WindowManager.LayoutParams.WRAP_CONTENT;
        }
        // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
        pop = new PopupWindow(popView, WindowManager.LayoutParams.MATCH_PARENT,
                popupWindowHeight, true);
        // 此处必须设置，否则点击事件无效，选择不了
        pop.setBackgroundDrawable(new BitmapDrawable());
        try {
            Method method = PopupWindow.class.getDeclaredMethod("setTouchModal", boolean.class);
            method.setAccessible(true);
            method.invoke(pop, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 设置显示动画
        //pop.setAnimationStyle(R.style.take_photo_anim);
        // 设置边缘点击可消失
        pop.setOutsideTouchable(true);
        //解决在EditText中输入把popupwindow布局往上顶的问题
        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        // 为了设置返回按钮关闭弹层
       // pop.setFocusable(true);
        pop.setTouchable(true); // pop 显示时， 不让外部 view 响应点击事件
        pop.update();
        WindowManager.LayoutParams params = activity.getWindow().getAttributes();
        params.alpha = 0.6f;
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        activity.getWindow().setAttributes(params);
        //将window视图显示在v的  指定位置
        pop.showAtLocation(popView, Gravity.BOTTOM, 0, 0);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                pop.dismiss();
                WindowManager.LayoutParams params = activity.getWindow().getAttributes();
                params.alpha = 1.0f;//设置为不透明，即恢复原来的界面
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                activity.getWindow().setAttributes(params);

            }
        });

    }
    /**
     * 显示PopupWindow  录音
     *
     * @param activity
     * @param popView     随便传一个您的界面有的View
     *
     * @param isAnimation 是否需要动画  true 为需要
     */
    public static void showPopupWindowRecprd(final Activity activity, View popView, boolean isAnimation) {
        hidePopupWindow();
        // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
        pop = new PopupWindow(popView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
        //此处为popwindow 设置背景，同事做到点击外部区域，popwindow消失
        // pop.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));
       // pop.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#60000000")));
        //pop.setFocusable(true);
        if (isAnimation) {
            //pop.setAnimationStyle(R.style.take_photo_anim);
        }
        //让键盘弹出时，不会挡住pop窗口。
        pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
       /* WindowManager.LayoutParams params = activity_imessage.getWindow().getAttributes();
        params.alpha = 0.6f;
        activity_imessage.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        activity_imessage.getWindow().setAttributes(params);*/
        //将window视图显示在v的  指定位置
        pop.showAtLocation(popView, Gravity.CENTER, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
               /* WindowManager.LayoutParams params = activity_imessage.getWindow().getAttributes();
                params.alpha = 1.0f;//设置为不透明，即恢复原来的界面
                activity_imessage.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                activity_imessage.getWindow().setAttributes(params);*/
                pop.dismiss();
            }
        });
    }
    /**
     * 显示PopupWindow
     *
     * @param activity
     * @param popView     随便传一个您的界面有的View
     * @param falg        点击外部区域，popwindow消失 true  为 消失
     * @param isAnimation 是否需要动画  true 为需要
     */
    public static void showPopupWindow(final Activity activity, View popView, boolean falg, boolean isAnimation) {
        hidePopupWindow();
        // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
        pop = new PopupWindow(popView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        //此处为popwindow 设置背景，同事做到点击外部区域，popwindow消失
        pop.setBackgroundDrawable(new ColorDrawable());
        pop.setFocusable(true);
        if (isAnimation) {
           // pop.setAnimationStyle(R.style.take_photo_anim);
        }
        //让键盘弹出时，不会挡住pop窗口。
        pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        WindowManager.LayoutParams params = activity.getWindow().getAttributes();
        params.alpha = 0.6f;
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        activity.getWindow().setAttributes(params);
        //将window视图显示在v的  指定位置
        pop.showAtLocation(popView, Gravity.CENTER, 0, 0);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = activity.getWindow().getAttributes();
                params.alpha = 1.0f;//设置为不透明，即恢复原来的界面
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                activity.getWindow().setAttributes(params);
                pop.dismiss();
            }
        });
    }
    /**
     * by lym
     * @param popView      要显示的view
     * @param relativeView 以该view的位置相对显示,为空就以屏幕位置显示
     * @param activity     activity_imessage
     * @param isfocusable  外部是否可点击
     * @param gravity      相对于屏幕的显示位置
     * @param width         popwindow的宽 ,如WindowManager.LayoutParams.WRAP_CONTENT||MATCH_PARENT
     * @param height        popwindow的高
     * @param offX         x轴偏移量
     * @param offY         y轴偏移量
     * @param alpha        外部的透明度参数0.1~1之间
     */
    public static void showPopupWindow(View popView, View relativeView, final Activity activity, boolean isfocusable,
                                       int gravity, int width, int height,
                                       int offX, int offY, float alpha) {
        pop = new PopupWindow(popView, width,
                height, isfocusable);
        WindowManager.LayoutParams params = activity.getWindow().getAttributes();//获取系统布局参数
        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setOutsideTouchable(isfocusable);
        //让键盘弹出时，不会挡住pop窗口。
        pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        params.alpha = alpha;//设置透明度参数
        activity.getWindow().setAttributes(params);//设置透明度
        if (relativeView != null) {//如果有,则以该控件位置偏移
            pop.showAsDropDown(relativeView, offX, offY);//显示位置和偏移量

        } else {//如果无,则以屏幕位置偏移
            pop.showAtLocation(popView, gravity, offX, offY);
        }
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {//pop关闭时监听
                WindowManager.LayoutParams params = activity.getWindow().getAttributes();
                params.alpha = 1.0f;//设置为透明，即恢复原来的界面
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                activity.getWindow().setAttributes(params);//重新设置透明度
                pop.dismiss();//关闭
            }
        });
    }



    public static void hidePopupWindows() {
        Log.i("TAG", "有没有进来消失的方法");
        if (popup!=null) {
            popup.dismiss();
            popup = null;
        }
    }

    /**
     *        ************自动识别图片专用**************
     *
     * @param popView      要显示的view
     * @param relativeView 以该view的位置相对显示,为空就以屏幕位置显示
     * @param activity     activity_imessage
     * @param isfocusable  外部是否可点击
     * @param gravity      相对于屏幕的显示位置
     * @param width         popwindow的宽 ,如WindowManager.LayoutParams.WRAP_CONTENT||MATCH_PARENT
     * @param height        popwindow的高
     * @param offX         x轴偏移量
     * @param offY         y轴偏移量
     * @param alpha        外部的透明度参数0.1~1之间
     */
    public static void showPopupWindows(View popView, View relativeView, Context context, final Activity activity, boolean isfocusable,
                                        int gravity, int width, int height, int offX, int offY, float alpha) {




       /* final PopupWindow pop = new PopupWindow(context);
        pop.setContentView(popView);
        pop.setWidth(width);
        pop.setHeight(height);
//相对某个控件的位置，有偏移;xoff表示x轴的偏移，正值表示向左，负值表示向右；yoff表示相对y轴的偏移，正值是向下，负值是向上；  
        pop.showAsDropDown(relativeView,offX,offY);
        pop.setOutsideTouchable(true);//设置外部点击消失

        WindowManager.LayoutParams params = activity_imessage.getWindow().getAttributes();//获取系统布局参数
        params.alpha = 1.0f;//设置透明度参数
        activity_imessage.getWindow().setAttributes(params);//设置透明度

        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {//在消失的时候让透明度恢复
            @Override
            public void onDismiss() {//pop关闭时监听
                WindowManager.LayoutParams params = activity_imessage.getWindow().getAttributes();
                params.alpha = 1.0f;//设置为透明，即恢复原来的界面
                activity_imessage.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                activity_imessage.getWindow().setAttributes(params);//重新设置透明度
                pop.dismiss();//关闭
            }
        });

        mHandler.sendEmptyMessageDelayed(1,5000);*/


        popup = new PopupWindow(context);
        popup.setContentView(popView);
        popup.setWidth(width);
        popup.setHeight(height);
        popup.setTouchable(true);
        popup.setFocusable(false);
        popup.setOutsideTouchable(true);


        popup.setBackgroundDrawable(activity.getResources().getDrawable(
                R.color.white));
        //popup.setBackgroundDrawable(new BitmapDrawable());

        WindowManager.LayoutParams params = activity.getWindow().getAttributes();//获取系统布局参数

        //让键盘弹出时，不会挡住pop窗口。
        popup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        popup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        params.alpha = alpha;//设置透明度参数
        activity.getWindow().setAttributes(params);//设置透明度
        popup.showAsDropDown(relativeView, offX, offY);//显示位置和偏移量
        if (relativeView != null) {//如果有,则以该控件位置偏移
            popup.showAsDropDown(relativeView, offX, offY);//显示位置和偏移量

        } else {//如果无,则以屏幕位置偏移
            popup.showAtLocation(popView, gravity, offX, offY);
        }
        popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {//pop关闭时监听
                WindowManager.LayoutParams params = activity.getWindow().getAttributes();
                params.alpha = 1.0f;//设置为透明，即恢复原来的界面
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                activity.getWindow().setAttributes(params);//重新设置透明度
                popup.dismiss();//关闭
            }
        });


    }




    /**
     * @param popView      要显示的view
     * @param relativeView 以该view的位置相对显示,为空就以屏幕位置显示
     * @param activity     activity_imessage
     * @param isfocusable  外部是否可点击
     * @param gravity      相对于屏幕的显示位置
     * @param offX         x轴偏移量
     * @param offY         y轴偏移量
     * @param alpha        外部的透明度参数0.1~1之间
     */
    public static void showPopupWindow(Context context, View popView, View relativeView, final Activity activity, boolean isfocusable,
                                       int gravity, int offX, int offY, float alpha) {
        pop = new PopupWindow(popView, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT, isfocusable);
        WindowManager.LayoutParams params = activity.getWindow().getAttributes();//获取系统布局参数
        pop.setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(R.color.white)));
        pop.setOutsideTouchable(isfocusable);
        params.alpha = alpha;//设置透明度参数
        activity.getWindow().setAttributes(params);//设置透明度
        if (relativeView != null) {//如果有,则以该控件位置偏移
            pop.showAsDropDown(relativeView, offX, offY);//显示位置和偏移量

        } else {//如果无,则以屏幕位置偏移
            pop.showAtLocation(popView, gravity, offX, offY);
        }
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {//pop关闭时监听
                WindowManager.LayoutParams params = activity.getWindow().getAttributes();
                params.alpha = 1.0f;//设置为透明，即恢复原来的界面
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                activity.getWindow().setAttributes(params);//重新设置透明度
                pop.dismiss();//关闭
            }
        });
    }

    /**
     * @param rootView 父控件
     * @param popView 弹窗内容
     * {新版登录注册页面弹窗}
     */
    public static void showPopupWindowBottom(View rootView , View popView, final Activity activity){
        pop = new PopupWindow(popView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        //pop.setContentView(popView);
       // pop.setAnimationStyle(R.style.Popupwindow);
        pop.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        WindowManager.LayoutParams params = activity.getWindow().getAttributes();//获取系统布局参数
        pop.setBackgroundDrawable(new ColorDrawable(activity.getResources().getColor(R.color.white)));
        params.alpha = 0.5f;//设置透明度参数
        activity.getWindow().setAttributes(params);//设置透明度
        pop.setOutsideTouchable(true);
//        pop.setFocusable(false);
        //显示PopupWindow
       // if(!activity.isFinishing()) {
            pop.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
       // }
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = activity.getWindow().getAttributes();
                params.alpha = 1.0f;//设置为透明，即恢复原来的界面
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                activity.getWindow().setAttributes(params);//重新设置透明度
                pop.dismiss();//关闭
            }
        });
    }
    public static void backgroundAlpha(float bgAlpha,Activity activity)
    {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        activity.getWindow().setAttributes(lp);
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    /**
     * 更新pop高度
     *
     * @param height
     */
    public static void offHeight(int height) {
        if (null != pop) {
            int width = pop.getWidth();
            pop.update(width, height);
        }
    }

    public static PopupWindow getPopupWindow() {
        if (null != pop){
            return pop;
        }
        return null;
    }
}
